﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.IO;
using MpSoft.Library.BUSS.Products;
using Newtonsoft.Json;

namespace MpSoft.Web.Services
{
    /// <summary>
    /// Summary description for AttributeResponse
    /// </summary>
    public class AttributeResponse : IHttpHandler
    {
        private string Action = string.Empty;
        private string Value = string.Empty, PAIDProdID = "";
        private int ObjID, AttributeValueID;
        private int SortOrder;
        private int ProductID;

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            LoadValue(context);
            ActionProdcess();
        }

        private void ActionProdcess()
        {
            var obj = AttributeValueBUSS.AttributeValue_ByID(ObjID);
            switch (Action)
            { 
                case "editvalue":
                    if (obj != null)
                    {
                        obj.Value = Value;
                        if (AttributeValueBUSS.AttributeValue_Update(obj) > 0)
                            HttpContext.Current.Response.Write("Lưu thành công");
                    }
                    break;
                case "delattvalue":
                    if (AttributeValueID>0)
                        if (AttributeValueBUSS.AttributeValue_Delete(AttributeValueID) > 0)
                            HttpContext.Current.Response.Write(1);
                    break;
                case "editorder":
                    if (obj != null)
                    {
                        int order;
                        int.TryParse(Value, out order);
                        obj.SortOrder = order;
                        if (AttributeValueBUSS.AttributeValue_Update(obj) > 0)
                            HttpContext.Current.Response.Write("Lưu thành công");
                    }
                    break;
                case "add":
                    obj = new Library.DTO.AttributeValue();
                    obj.AttributeID = ObjID;
                    obj.Value = Value;
                    obj.SortOrder = SortOrder;
                    ObjID = AttributeValueBUSS.AttributeValue_Add(obj);
                    if (ObjID > 0)
                        HttpContext.Current.Response.Write(ObjID);
                    break;
                case "listattrvalue":
                    HttpContext.Current.Response.Write(JsonConvert.SerializeObject(AttributeValueBUSS.AttributeValue_ByAttributeID(ObjID)));
                    break;  
                case "editattval1":
                    string[] _PAIDProdID = PAIDProdID.Split(',');
                    if (_PAIDProdID.Length > 0)
                    {
                        int PAID, ProdID;
                        int.TryParse(_PAIDProdID[0], out PAID);
                        int.TryParse(_PAIDProdID[1], out ProdID);
                        if (AttributeValueBUSS.ProductAttribute_Select(ProdID, PAID, 1) != null)
                        {
                            HttpContext.Current.Response.Write(-2);
                            break;
                        }
                        try
                        {
                            AttributeValueBUSS.ProductAttribute_Update(PAID, int.Parse(Value), ProdID);
                            HttpContext.Current.Response.Write(1);
                        }
                        catch { HttpContext.Current.Response.Write(-1); }
                    }
                    break;
                case "addattrprod":
                    if (AttributeValueBUSS.ProductAttribute_Select(ProductID, ObjID, 1) != null)
                    {
                        HttpContext.Current.Response.Write(-1);
                        break;
                    }
                    ObjID = AttributeValueBUSS.ProductAttribute_Add(-1, ObjID, ProductID);
                    if (ObjID > 0)
                    {
                        HttpContext.Current.Response.Write(ObjID);
                        break;
                    }
                       
                    HttpContext.Current.Response.Write(-1);
                    break;
                case "delattrprod":
                    if (ObjID > 0)
                        HttpContext.Current.Response.Write(AttributeValueBUSS.ProductAttribute_Delete(ObjID));
                    break;
            }
        }

        private void LoadValue(HttpContext context)
        {
            if (context.Request.Form["name"] != null)
                Action = context.Request.Form["name"].ToString();
            if (context.Request.Form["value"] != null)
                Value = context.Request.Form["value"].ToString();
            if (context.Request.Form["pk"] != null)
                int.TryParse(context.Request.Form["pk"].ToString(), out SortOrder);
            if (context.Request.Form["pk"] != null)
                PAIDProdID = context.Request.Form["pk"].ToString();
            if (context.Request.Form["AID"] != null)
                int.TryParse(context.Request.Form["AID"].ToString(), out ObjID);
            if (context.Request.Form["prodid"] != null)
                int.TryParse(context.Request.Form["prodid"].ToString(), out ProductID);
            if (context.Request.Form["delattvalue"] != null)
                int.TryParse(context.Request.Form["delattvalue"].ToString(), out AttributeValueID);
        }

        //public void ProcessRequest(HttpContext context)
        //{
        //    context.Response.ContentType = "text/plain";
        //    string htmlString = RenderHtml("~/Controls/Control.ascx");
        //    context.Response.Write(htmlString);
        //}
        private string RenderHtml(string controlName)
        {
            Page page = new Page();
            Control control = page.LoadControl(controlName);
            HtmlForm form = new HtmlForm();
            form.Controls.Add(control);
            page.Controls.Add(form);
            StringWriter writer = new StringWriter();
            HttpContext.Current.Server.Execute(page, writer, false);
            return writer.ToString();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        
    }
}